package com.fj.netty.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

import java.nio.charset.Charset;
import java.util.Arrays;

/**
 * @author fj
 * @date 2024/1/8 11:22
 */
public class ZeroCopy {
    public static void main(String[] args) {
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
        System.out.println(byteBuf);
        String val = "hello world";
        byteBuf.writeBytes(val.getBytes(Charset.defaultCharset()));
        System.out.println(byteBuf);
        System.out.println(byteBuf.getByte(0));
        System.out.println(byteBuf);
        System.out.println(byteBuf.readByte());
        System.out.println(byteBuf);
        System.out.println(byteBuf.readByte());
        System.out.println(byteBuf);
        ByteBuf slice = byteBuf.slice(3,8);
        System.out.println(slice.readByte());
        System.out.println(slice.toString(Charset.defaultCharset()));
        System.out.println(byteBuf.toString(Charset.defaultCharset()));
        byteBuf.setByte(5,'x');
        System.out.println(slice.toString(Charset.defaultCharset()));
        System.out.println(byteBuf.toString(Charset.defaultCharset()));
        byteBuf.retain();
        byteBuf.release();
        System.out.println(slice.toString(Charset.defaultCharset()));

//        ByteBuf duplicate = byteBuf.duplicate();
//        System.out.println(duplicate.toString(Charset.defaultCharset()));
    }
}
